﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Outpatient.Infrastructure
{
    /// <summary>
    /// 基础仓储
    /// </summary>
    public interface IBaseRepository<T> where T : class
    {
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        Task<int> AddAsyn(T t);
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        IQueryable<T> Getlist();
        
        /// <summary>
        /// 安全查询，处理可能的NULL值
        /// </summary>
        /// <returns>安全的查询对象</returns>
        IQueryable<T> GetSafeList();
        
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        Task<int> Update(T t);

        /// <summary>
        /// 根据条件查找信息
        /// </summary>
        /// <param name="exp"></param>
        /// <returns></returns>
        Task<T> GetValue(Expression<Func<T, bool>> exp);

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        Task<int> Batchadd(List<T> t);

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        Task<int> BatchUpdate(List<T> t);
        
        /// <summary>
        /// 异步批量添加
        /// </summary>
        /// <param name="entities">实体列表</param>
        /// <returns>任务</returns>
        Task AddRangeAsync(List<T> entities);
        
        /// <summary>
        /// 保存更改到数据库
        /// </summary>
        /// <returns>受影响的行数</returns>
        Task<int> SaveAsync();
        
        /// <summary>
        /// 获取数据库上下文
        /// </summary>
        /// <returns>数据库上下文</returns>
        DbContext GetDbContext();
        
        /// <summary>
        /// 安全执行SQL查询，避免NULL转换异常
        /// </summary>
        /// <typeparam name="TResult">返回类型</typeparam>
        /// <param name="sql">SQL查询语句</param>
        /// <param name="parameters">查询参数</param>
        /// <returns>查询结果</returns>
        Task<List<TResult>> ExecuteSafeSqlQuery<TResult>(string sql, params object[] parameters) where TResult : class, new();
    }
}
